package com.tjec.project.projectWorkbench.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.tjec.project.projectWorkbench.mapper.BmsProjectLibraryMapper;
import com.tjec.common.constant.Constants;
import com.tjec.common.utils.ServletUtils;
import com.tjec.common.utils.StringUtils;
import com.tjec.framework.security.service.TokenService;
import com.tjec.project.projectWorkbench.domain.BmsProjectContract;
import com.tjec.project.projectWorkbench.domain.BmsProjectLibrary;
import com.tjec.project.projectWorkbench.service.IBmsProjectLibraryService;
import com.tjec.project.system.domain.BmsAttachment;
import com.tjec.project.system.mapper.BmsAttachmentMapper;

/**
 * 精品项目库Service业务层处理
 * 
 * @author pangyongfeng
 * @date 2021-01-18
 */
@Service
public class BmsProjectLibraryServiceImpl implements IBmsProjectLibraryService {
	@Autowired
	private BmsProjectLibraryMapper bmsProjectLibraryMapper;

	@Autowired
	private BmsAttachmentMapper bmsAttachmentMapper;

	@Autowired
	private TokenService tokenService;

	@Value("${sftp.http}")
	private String http;
	@Value("${sftp.ip}")
	private String ip;

	/**
	 * 查询精品项目库
	 * 
	 * @param guid 精品项目库ID
	 * @return 精品项目库
	 */
	@Override
	public BmsProjectLibrary selectBmsProjectLibraryByGuid(String guid) {
		return bmsProjectLibraryMapper.selectBmsProjectLibraryByGuid(guid);
	}

	/**
	 * 查询精品项目库
	 * 
	 * @param projectId 项目ID
	 * @return 精品项目库
	 */
	@Override
	public BmsProjectLibrary selectBmsProjectLibraryByProjectId(String projectId) {
		BmsProjectLibrary bmsProjectLibrary = bmsProjectLibraryMapper.selectBmsProjectLibraryByProjectId(projectId);
		if (bmsProjectLibrary != null) {
			addAttachment(bmsProjectLibrary);
		}

		return bmsProjectLibrary;
	}

	// 项目合同附件
	private void addAttachment(BmsProjectLibrary bmsProjectLibrary) {
		ArrayList<String> attIds = new ArrayList<>();
		if (StringUtils.isNotBlank(bmsProjectLibrary.getProjectAttachment())) {
			List<BmsAttachment> attachments = bmsAttachmentMapper
					.selectAttachmentsByBizIds(bmsProjectLibrary.getProjectAttachment());
			// 附件
			if (!CollectionUtils.isEmpty(attachments)) {
				bmsProjectLibrary.setBmsAttachments(attachments);
				for (BmsAttachment bmsAttachment : attachments) {
					bmsAttachment
							.setStoragePath(http + ip + Constants.RESOURCE_PREFIX + bmsAttachment.getStoragePath());
					attIds.add(bmsAttachment.getGuid());
				}
			}
			bmsProjectLibrary.setAttachmentIds(attIds);
		}

		if (StringUtils.isNotBlank(bmsProjectLibrary.getProjectShowMainImage())) {
			List<BmsAttachment> showMainImageAtts = bmsAttachmentMapper
					.selectAttachmentsByBizIds(bmsProjectLibrary.getProjectShowMainImage());
			// 项目展示主图
			if (!CollectionUtils.isEmpty(showMainImageAtts)) {
				bmsProjectLibrary.setProjectShowMainImages(showMainImageAtts);
				for (BmsAttachment bmsAttachment : showMainImageAtts) {
					bmsAttachment
							.setStoragePath(http + ip + Constants.RESOURCE_PREFIX + bmsAttachment.getStoragePath());
				}
			}
		}

		if (StringUtils.isNotBlank(bmsProjectLibrary.getProjectShowImage())) {
			List<BmsAttachment> showImageAtts = bmsAttachmentMapper
					.selectAttachmentsByBizIds(bmsProjectLibrary.getProjectShowImage());
			// 项目展示图片
			if (!CollectionUtils.isEmpty(showImageAtts)) {
				bmsProjectLibrary.setProjectShowImages(showImageAtts);
				for (BmsAttachment bmsAttachment : showImageAtts) {
					bmsAttachment
							.setStoragePath(http + ip + Constants.RESOURCE_PREFIX + bmsAttachment.getStoragePath());
				}
			}
		}

		if (StringUtils.isNotBlank(bmsProjectLibrary.getStoryImage())) {
			List<BmsAttachment> shoryImageAtts = bmsAttachmentMapper
					.selectAttachmentsByBizIds(bmsProjectLibrary.getStoryImage());
			// 故事图片
			if (!CollectionUtils.isEmpty(shoryImageAtts)) {
				bmsProjectLibrary.setStoryImages(shoryImageAtts);
				for (BmsAttachment bmsAttachment : shoryImageAtts) {
					bmsAttachment
							.setStoragePath(http + ip + Constants.RESOURCE_PREFIX + bmsAttachment.getStoragePath());
				}
			}
		}
	}

	/**
	 * 查询精品项目库列表
	 * 
	 * @param bmsProjectLibrary 精品项目库
	 * @return 精品项目库
	 */
	@Override
	public List<BmsProjectLibrary> selectBmsProjectLibraryList(BmsProjectLibrary bmsProjectLibrary) {
		return bmsProjectLibraryMapper.selectBmsProjectLibraryList(bmsProjectLibrary);
	}

	/**
	 * 新增精品项目库
	 * 
	 * @param bmsProjectLibrary 精品项目库
	 * @return 结果
	 */
	@Override
	public int insertBmsProjectLibrary(BmsProjectLibrary bmsProjectLibrary) {
		// 附件
		if (bmsProjectLibrary.getAttachmentIds() != null && bmsProjectLibrary.getAttachmentIds().size() > 0) {
			bmsProjectLibrary.setProjectAttachment(StringUtils.join(bmsProjectLibrary.getAttachmentIds(), ","));
		} else {
			bmsProjectLibrary.setProjectAttachment("");
		}

		// 项目展示主图
		if (bmsProjectLibrary.getProjectShowMainImages() != null
				&& bmsProjectLibrary.getProjectShowMainImages().size() > 0) {
			bmsProjectLibrary.setProjectShowMainImage(bmsProjectLibrary.getProjectShowMainImages().get(0).getGuid());
		}

		// 项目展示图片
		if (bmsProjectLibrary.getProjectShowImages() != null && bmsProjectLibrary.getProjectShowImages().size() > 0) {
			bmsProjectLibrary.setProjectShowImage(bmsProjectLibrary.getProjectShowImages().get(0).getGuid());
		}

		// 故事图片
		if (bmsProjectLibrary.getStoryImages() != null && bmsProjectLibrary.getStoryImages().size() > 0) {
			bmsProjectLibrary.setStoryImage(bmsProjectLibrary.getStoryImages().get(0).getGuid());
		}

		return bmsProjectLibraryMapper.insertBmsProjectLibrary(bmsProjectLibrary);
	}

	/**
	 * 修改精品项目库
	 * 
	 * @param bmsProjectLibrary 精品项目库
	 * @return 结果
	 */
	@Override
	@Transactional
	public int updateBmsProjectLibrary(BmsProjectLibrary bmsProjectLibrary) {
		BmsProjectLibrary oldProjectLibrary = bmsProjectLibraryMapper
				.selectBmsProjectLibraryByGuid(bmsProjectLibrary.getGuid());
		// 附件
		if (StringUtils.isNotBlank(oldProjectLibrary.getProjectAttachment())) {
			bmsAttachmentMapper.updateBmsAttachmentByGuids(oldProjectLibrary.getProjectAttachment().split(","),
					tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
		}

		// 项目展示主图
		if (bmsProjectLibrary.getProjectShowMainImages() != null
				&& bmsProjectLibrary.getProjectShowMainImages().size() > 0) {
			bmsProjectLibrary.setProjectShowMainImage(bmsProjectLibrary.getProjectShowMainImages().get(0).getGuid());
			if (oldProjectLibrary.getProjectShowMainImage() != null && !(oldProjectLibrary.getProjectShowMainImage())
					.equals(bmsProjectLibrary.getProjectShowMainImages().get(0).getGuid())) {
				bmsAttachmentMapper.updateBmsAttachmentByGuids(oldProjectLibrary.getProjectShowMainImage().split(","),
						tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
			}
		}

		// 项目展示图片
		if (bmsProjectLibrary.getProjectShowImages() != null && bmsProjectLibrary.getProjectShowImages().size() > 0) {
			bmsProjectLibrary.setProjectShowImage(bmsProjectLibrary.getProjectShowImages().get(0).getGuid());
			if (oldProjectLibrary.getProjectShowImage() != null && !(oldProjectLibrary.getProjectShowImage())
					.equals(bmsProjectLibrary.getProjectShowImages().get(0).getGuid())) {
				bmsAttachmentMapper.updateBmsAttachmentByGuids(oldProjectLibrary.getProjectShowImage().split(","),
						tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
			}
		} else {
			bmsProjectLibrary.setProjectShowImage("");
		}

		// 故事图片
		if (bmsProjectLibrary.getStoryImages() != null && bmsProjectLibrary.getStoryImages().size() > 0) {
			bmsProjectLibrary.setStoryImage(bmsProjectLibrary.getStoryImages().get(0).getGuid());
			if (oldProjectLibrary.getStoryImage() != null && !(oldProjectLibrary.getStoryImage())
					.equals(bmsProjectLibrary.getStoryImages().get(0).getGuid())) {
				bmsAttachmentMapper.updateBmsAttachmentByGuids(oldProjectLibrary.getStoryImage().split(","),
						tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
			}
		} else {
			bmsProjectLibrary.setStoryImage("");
		}

		if (bmsProjectLibrary.getAttachmentIds() != null && bmsProjectLibrary.getAttachmentIds().size() > 0) {
			bmsProjectLibrary.setProjectAttachment(StringUtils.join(bmsProjectLibrary.getAttachmentIds(), ","));
		} else {
			bmsProjectLibrary.setProjectAttachment("");
		}

		return bmsProjectLibraryMapper.updateBmsProjectLibrary(bmsProjectLibrary);
	}

	/**
	 * 批量删除精品项目库
	 * 
	 * @param guids 需要删除的精品项目库ID
	 * @return 结果
	 */
	@Override
	public int deleteBmsProjectLibraryByGuids(String[] guids) {
		return bmsProjectLibraryMapper.deleteBmsProjectLibraryByGuids(guids);
	}

	/**
	 * 删除精品项目库信息
	 * 
	 * @param guid 精品项目库ID
	 * @return 结果
	 */
	@Override
	public int deleteBmsProjectLibraryByGuid(String guid) {
		return bmsProjectLibraryMapper.deleteBmsProjectLibraryByGuid(guid);
	}
}
